package com.hlm.transparent.util;

//import cn.qdhlm.hems.inter.nb.cloud.common.Constant;
import io.netty.buffer.ByteBuf;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @Name: ComUtil
 * @Description: ComUtil
 * @Author: jiangcs
 **/
public class ComUtil {
    public static boolean isEmpty(Object object) {
        return StringUtils.isEmpty(object);
    }

    /**
     * isZero
     *
     * @param object
     * @return
     */
    public static boolean isZero(Object object) {
        return safeInt(object) == 0;
    }

    /**
     * is contain
     *
     * @param source
     * @param key
     * @return
     */
    /*public static boolean isContain(Object source, Object key) {
        if (!cn.qdhlm.hems.inter.nb.cloud.common.util.ComUtil.isEmpty(source) && !cn.qdhlm.hems.inter.nb.cloud.common.util.ComUtil.isEmpty(key)) {
            if (source.getClass().isArray()) {
                Object[] sourceArr = (Object[]) source;
                for (int i = 0; i < sourceArr.length; i++) {
                    if (key.equals(sourceArr[i])) {
                        return true;
                    }
                }
            } else {
                return ("," + source + ",").contains("," + key + ",");
            }
        }
        return false;
    }*/

    /**
     * safeString
     *
     * @param object
     * @return
     */
    public static String safeString(Object object) {
        if (isEmpty(object)) {
            return "";
        }
        return String.valueOf(object);
    }

    /**
     * safeInt
     *
     * @param object
     * @return
     */
    public static int safeInt(Object object) {
        if (isEmpty(object)) {
            return 0;
        }
        try {
            if (object instanceof Double) {
                return ((Double) object).intValue();
            }
            if (object instanceof Float) {
                return ((Float) object).intValue();
            }
            return Integer.parseInt(safeString(object));
        } catch (Exception e) {
            return 0;
        }
    }

    /**
     * Object 2 Double
     *
     * @param object
     * @return
     */
    public static double safeDouble(Object object) {
        if (isEmpty(object)) {
            return 0;
        }
        try {
            return Double.parseDouble(safeString(object));
        } catch (Exception e) {
            return 0D;
        }
    }

    /**
     * Object 2 Long
     *
     * @param object
     * @return
     */
    public static long safeLong(Object object) {
        if (isEmpty(object)) {
            return 0;
        }
        try {
            return Long.parseLong(safeString(object));
        } catch (Exception e) {
            return 0L;
        }
    }

    /**
     * 格式化日期时间
     *
     * @param pattern
     * @param date
     * @return
     */
    public static String formatDateTime(Date date, String pattern) {
        if (isEmpty(date)) {
            return "";
        }
        if (isEmpty(pattern)) {
            pattern = "yyyy-MM-dd HH:mm:ss";
        }
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        return sdf.format(date);
    }

    /**
     * 根据length 左补零
     *
     * @param object
     * @param length
     * @return
     */
    public static String fillZeroLeft(Object object, long length) {
        String formatter = "%0" + length + "d";
        return String.format(formatter, safeInt(object));
    }

    /**
     * 格式化 Double类型 小数
     *
     * @param d
     * @param scale
     * @return
     */
    public static Double formatScale(Double d, int scale) {
        BigDecimal bigDecimal = BigDecimal.valueOf(d).setScale(scale, RoundingMode.HALF_UP);
        return bigDecimal.doubleValue();
    }

    /**
     * byte 2 double
     *
     * @param b
     * @param d
     * @param point
     * @return
     */
    public static double doubleConvert(byte b, Double d, int point) {
        return formatScale(b * Math.pow(d, point), point);
    }

    /**
     * short 2 double
     *
     * @param s
     * @param d
     * @param point
     * @return
     */
    public static double doubleConvert(short s, Double d, int point) {
        return formatScale(s * Math.pow(d, point), point);
    }

    /**
     * int 2 double
     *
     * @param i
     * @param d
     * @param point
     * @return
     */
    public static double doubleConvert(int i, Double d, int point) {
        return formatScale(i * Math.pow(d, point), point);
    }

    /**
     * 单位 转换
     *
     * @param d
     * @param unit
     * @param protocolCode
     * @return
     */
    public static double unitConvert(double d, int unit, String protocolCode) {
        double num = 1;
        // 京源热表(NB) || 京源IC卡阀控热表(NB) || 京源大口径热表(NB)
        if ("CJT188_0033".equals(protocolCode) || "CJT188_0053".equals(protocolCode) || "CJT188_0060".equals(protocolCode)) {
            // 转换KWH
            if (unit > 2 && unit <= 10) {
                num *= Math.pow(10, unit - 2) * 0.001;
            }
            // j转换成kw
            else if (unit >= 11 && unit <= 17) {
                num *= Math.pow(10, unit - 11) * 0.001 / 3.6;
            }
            // 转换成kw
            else if (unit >= 19 && unit <= 28) {
                num *= Math.pow(10, unit - 19) * 0.0001;
            }
            // 转成M3/H
            else if (unit == 32) {
                num *= 0.001;
            }
            // 转成M3
            else if (unit >= 39 && unit <= 44) {
                num *= Math.pow(10, unit - 39) * 0.00001;
            }
            // 转成M3/H
            else if (unit >= 46 && unit <= 50) {
                num *= Math.pow(10, unit - 46) * 0.0001;
            }
        }
        // 国标
        else {
            // 转换KWH
            if (unit > 2 && unit <= 10) {
                num *= Math.pow(10, unit - 2) * 0.001;
            }
            // j转换成kw
            else if (unit >= 11 && unit <= 19) {
                num *= Math.pow(10, unit - 11) * 0.001 / 3.6;
            }
            // 转换成kw
            else if (unit >= 20 && unit <= 28) {
                num *= Math.pow(10, unit - 20) * 0.001;
            }
            // 转成M3
            else if (unit >= 41 && unit <= 46) {
                num *= Math.pow(10, unit - 41) * 0.001;
            }
            // 转成M3/H
            else if (unit >= 50 && unit <= 55) {
                num *= Math.pow(10, unit - 50) * 0.001;
            }
        }
        return num * d;
    }

    /**
     * 跳过68之前的FE
     *
     * @param byteBuf
     * @return
     */
    public static int skipFE(ByteBuf byteBuf) {
        int start= 0;
        for (int i = 0 ; i < byteBuf.readableBytes() ; i ++){
            if ((byte)0xFE!= byteBuf.getByte(i)){
                start = i ;
                break;
            }
        }
        return start;
    }

    // modbus CRC校验和
    public static int modbusCRC(byte[] bytes) {
        int crc = 0x0000ffff;
        for (int i = 0; i < bytes.length; i++) {
            crc ^= ((int) bytes[i] & 0x000000ff);
            for (int j = 0; j < 8; j++) {
                if ((crc & 0x00000001) != 0) {
                    crc >>= 1;
                    crc ^= 0x0000a001;
                } else {
                    crc >>= 1;
                }
            }
        }
        //高低位互换，输出符合相关工具对Modbus CRC16的运算
        crc = ( (crc & 0xff00) >> 8) | ( (crc & 0x00ff ) << 8);
        return crc;
    }
}
